﻿using System;
using System.IO;
using System.Windows.Controls;
using MyFloridaHouse.libSvUtilities.Logging;

namespace MyFloridaHouse.libSvUtilities
{
    public delegate MemoryStream OutputFileGenerator(string safeFileName);

    public static class LocalFileManager
    {

        public static bool SaveFileLocal(OutputFileGenerator outputFileGenerator, string filter, string defaultExtension)
        {
            bool result = false;
            if (outputFileGenerator != null)
            {
                Stream fs = null ;
                string fileName;
                try
                {
                    if (LocalFileManager.ShowSaveDialog(filter, defaultExtension, out fileName, out fs))
                    {
                        if (fs != null)
                        {
                            using (MemoryStream fileMemoryStream = outputFileGenerator(fileName))
                            {
                                using (fs)
                                {
                                    fileMemoryStream.WriteTo(fs);
                                }
                                fileMemoryStream.Close();
                            }
                            result = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (fs != null)
                    {
                        fs.Close();
                    }
                }

            }
            return result;


        }

        public static bool ShowSaveDialog(string filter, string defaultExtension, out string fileName, out Stream fs)
        {
            bool result = false;
            fs = null;
            fileName = "";
            try
            {
                SaveFileDialog textDialog = new SaveFileDialog();
                textDialog.Filter = filter;
                textDialog.DefaultExt = defaultExtension;

                result = (bool) textDialog.ShowDialog();

                if (result == true)
                {
                    fs = textDialog.OpenFile();
                    fileName = textDialog.SafeFileName;
                }

                return result;
            }
            catch (Exception ex)
            {
                Log.HandleInternalError(ex.ToString());
                return false;
            }
        }


        public static bool ReadFileLocal(string filter, out FileInfo fileInfo, out FileStream fileStream)
        {
            bool result = false;
            fileInfo = null;
            fileStream = null;
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = filter;
                openFileDialog.Multiselect = false;

                result = (bool) openFileDialog.ShowDialog();

                if (result == true)
                {

                    fileInfo = openFileDialog.File;
                    fileStream = openFileDialog.File.OpenRead();
                }
                return result;
            }
            catch (Exception ex)
            {
                Log.HandleInternalError(ex.ToString());
                return false;
            }
        }


        //public static StreamReader Read(string filter, out FileInfo fileInfo)
        //{
        //    try
        //    {
        //        OpenFileDialog openFileDialog = new OpenFileDialog();
        //        openFileDialog.Filter = filter;
        //        openFileDialog.Multiselect = false;

        //        bool? result = openFileDialog.ShowDialog();

        //        if (result == true)
        //        {
                    
        //            fileInfo = openFileDialog.File;
        //            FileStream fileStream = openFileDialog.File.OpenRead();
        //            StreamReader sr = new StreamReader(fileStream);
        //            return sr;
        //        }
        //        else
        //        {
        //            fileInfo = null;
        //            return null;
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        Log.HandleInternalError(ex.ToString());
        //        fileInfo = null;
        //        return null;
        //    }
        //}

        //public static bool SaveData(byte[] buff, string fileName)
        //{
        //    bool response = false;
        //    try
        //    {
        //        FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
        //        BinaryWriter bw = new BinaryWriter(fs);
        //        bw.Write(buff);
        //        bw.Close();
        //        response = true;
        //    }
        //    catch (Exception ex)
        //    {
        //        string theMsg = ex.ToString();
        //        throw;
        //    }
        //    return response;
        //}

        //public static void LoadData(string data, string fileName)
        //{

        //}

    }
}
